package partlow.googleplaces.parsers;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import partlow.googleplaces.datatypes.GooglePlacesType;

/**
 *
 * @author Devin
 */
public abstract class GooglePlacesTypeParser<T extends GooglePlacesType> {
    public abstract T parse(JSONObject json) throws JSONException;
    public List<T> parse(JSONArray array) throws JSONException{
        List<T> list = new ArrayList<T>();
        for (int i = 0, m = array.length(); i < m; i++) {
            Object element = array.get(i);
            T item = null;
            if (element instanceof JSONArray) {
                //item = parse((JSONArray)element);
            } else {
                item = parse((JSONObject)element);
            }
            
            list.add(item);
        }
        return list;
    }
    
    protected Date parseDate(String dateString) throws ParseException{
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = (Date)formatter.parse(dateString.replace("T", " ").replace("Z", ""));
        
        return date;
    }
}
